import { TCatalogData, TProductData, TState } from './storeProvider'

export enum Actions {
	SET_ALL_DATA = 'SET_ALL_DATA',
	SET_CATALOG_DATA = 'SET_CATALOG_DATA',
	SET_PRODUCT_DATA = 'SET_PRODUCT_DATA',
	SET_PENDING = 'SET_PENDING',
	SET_SEARCH = 'SET_SEARCH',
	SET_PAGE = 'SET_PAGE',
	SET_MAX_PAGE = 'SET_MAX_PAGE',
}

export type TReduceActions =
	| { type: Actions.SET_ALL_DATA; allData: TCatalogData[] }
	| { type: Actions.SET_CATALOG_DATA; catalogData: TCatalogData[] }
	| { type: Actions.SET_PRODUCT_DATA; productData: TProductData }
	| {
			type: Actions.SET_PENDING
			pending: boolean
	  }
	| { type: Actions.SET_SEARCH; search: string }
	| { type: Actions.SET_PAGE; selectedPage: number }
	| { type: Actions.SET_MAX_PAGE; maxPage: number }

const reducer = (state: TState, action: TReduceActions) => {
	switch (action.type) {
		case 'SET_ALL_DATA':
			return {
				...state,
				allData: action.allData,
			}
		case 'SET_CATALOG_DATA':
			return {
				...state,
				catalogData: action.catalogData,
			}
		case 'SET_PRODUCT_DATA':
			return {
				...state,
				productData: action.productData,
			}
		case 'SET_PENDING':
			return {
				...state,
				pending: action.pending,
			}
		case 'SET_SEARCH':
			return {
				...state,
				search: action.search,
			}
		case 'SET_PAGE':
			return {
				...state,
				pagination: { ...state.pagination, selectedPage: action.selectedPage },
			}
		case 'SET_MAX_PAGE':
			return {
				...state,
				pagination: { ...state.pagination, maxPage: action.maxPage },
			}
		default:
			return state
	}
}

export default reducer
